package com.micro.service.auth.server.config.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.micro.service.auth.server.config.properties.SecurityProperties;
import com.micro.service.common.dto.ResultDTO;
import com.micro.service.common.enums.LoginType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lvxiucai
 * @description 登录失败处理
 * @date 2019/7/16
 */

@Slf4j
@Component("loginFailureHandler")
public class LoginFailureHandler extends SimpleUrlAuthenticationFailureHandler {

    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private SecurityProperties securityProperties;


    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        ResultDTO resultDTO = new ResultDTO();
        log.error("用户登录失败");
        if (exception instanceof UsernameNotFoundException) {
            log.error("用户不存在{}",request.getParameter("user"));
            response.setStatus(HttpServletResponse.SC_OK);
            response.setContentType("application/json;charset=UTF-8");
            resultDTO.failure("用户不存在");
            response.getWriter().write(objectMapper.writeValueAsString(resultDTO));
        }else if(exception instanceof BadCredentialsException){
            log.error("登录失败{}", exception.getMessage());
            response.setStatus(HttpServletResponse.SC_OK);
            response.setContentType("application/json;charset=UTF-8");
            resultDTO.failure(exception.getMessage());
            response.getWriter().write(objectMapper.writeValueAsString(resultDTO));
        }else {
            if(LoginType.JSON.equals(securityProperties.getBrowserProperties().getLoginType())){
                response.setStatus(HttpServletResponse.SC_OK);
                response.setContentType("application/json;charset=UTF-8");
                resultDTO.failure(exception.getMessage());
                log.error("失败原因:{}", exception.getMessage());
                response.getWriter().write(objectMapper.writeValueAsString(resultDTO));
            }else {
                redirectStrategy.sendRedirect(request,response,securityProperties.getBrowserProperties().getLoginFailurePage());
            }
        }

    }
}
